package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import student.Student;
import student.StudentDao;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		request.getParameter("stuno");
		Student stu = new Student();
		StudentDao sd = new StudentDao();
		stu.setStuno(Long.parseLong(request.getParameter("stuno")));
		stu.setSname(request.getParameter("sname"));
		stu.setSex(request.getParameter("sex"));
		stu.setTelephone(request.getParameter("telephone"));
		stu.setQq(request.getParameter("qq"));
		stu.setWechat(request.getParameter("wechat"));
		stu.setGradeId(Long.parseLong(request.getParameter("gradeid")));
		try {
			stu.setEnterDate(sdf.parse(request.getParameter("enterdate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		stu.setFromcity(request.getParameter("fromcity"));
		try {
			stu.setBirthday(sdf.parse(request.getParameter("birthday")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		stu.setAddress(request.getParameter("address"));
		stu.setPass(request.getParameter("pass"));
		int n = sd.newUpdate(stu);
		out.print("修改成功<br/>" + stu.toShow() + "<br/><a href='SelectServlet'>修改成功，返回首页</a>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
